# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:yi_search2_0.py
@Time:2024/8/21 15:49

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""

from yi.yi_class1_0 import yi_dict
import streamlit as st

data = yi_dict

def search_keyword(keyword):
    results = []
    current_gua = None
    for gua, lines in data.items():
        for line in lines:
            if keyword in line:
                if gua!= current_gua:
                    results.append(gua)
                    current_gua = gua
                results.append(line)
    return results


def yisearch():
    st.title('易经关键词查找')

    keyword = st.text_input('请输入关键词：', value='无咎')

    if keyword:
        results = search_keyword(keyword)
        if results:
            # print(results)
            st.write(f'找到{len(results)-sum(1 for item in results if len(item)<=2)}条匹配结果：')
            current_gua = None
            for result in results:
                if isinstance(result, str):
                    st.markdown(f'##### {result}')
                else:
                    if current_gua!= result:
                        st.write(f'卦：{result}')
                    current_gua = result
        else:
            st.write('未找到匹配结果。')